/*
 * @Author: yuluoY 2120942398@qq.com
 * @Date: 2024-10-28 20:53:25
 * @LastEditors: yuluoY 2120942398@qq.com
 * @LastEditTime: 2024-10-31 20:56:06
 * @FilePath: \Projecte:\gitee\GD32F427\gd32-f427\App\src\app_led.c
 * @Description: 
 * 
 * Copyright (c) 2024 by ${YuluoY}, All Rights Reserved. 
 */
#include "main.h"

led_info_t led_R_info, led_G_info;    /* led信息结构体 */
led_ctrl_t LED_RED, LED_GREEN;              /* led控制结构体 */


/**
 * @Author: YuluoY
 * @Date: 2024-10-29 20:45:27
 * @LastEditors: yuluoY 2120942398@qq.com
 * @LastEditTime: Do not edit
 * @Description: led创建函数；初始化
 * @return {}
 */
void app_led_create(void)
{
    drv_led_info_init(&led_R_info, RCU_GPIOF, GPIOF, GPIO_PIN_9, SET, true);
    drv_led_init(&led_R_info, &LED_RED);
    
    drv_led_info_init(&led_G_info, RCU_GPIOF, GPIOF, GPIO_PIN_10, SET, true);
    drv_led_init(&led_G_info, &LED_GREEN);
}

/**
 * @Author: YuluoY
 * @Date: 2024-10-29 20:45:10
 * @LastEditors: yuluoY 2120942398@qq.com
 * @LastEditTime: Do not edit
 * @Description: led设置电平函数
 * @param {led_info_t} *led_info
 * @param {led_ctrl_t} *led_ctrl
 * @param {bool} new_level
 * @return {bool}   {true 成功} {false 失败}
 */
bool led_set_level(led_info_t *led_info, led_ctrl_t *led_ctrl, bool new_level)
{
    led_ctrl->Current_Status = new_level;
    // return drv_led_on_off(led_info, led_ctrl);
    return true;
}

/**
 * @description: 
 * @param {led_info_t} *led_info
 * @param {led_ctrl_t} *led_ctrl
 * @param {bool} new_level
 * @return {*}
 */
void led_set(led_info_t *led_info, led_ctrl_t *led_ctrl, bool new_level)
{
    gpio_bit_write(led_info->Port, led_info->Pin, (bit_status)new_level);
}

/**
 * @Author: YuluoY
 * @Date: 2024-10-29 20:44:57
 * @LastEditors: yuluoY 2120942398@qq.com
 * @LastEditTime: Do not edit
 * @Description: led电平翻转函数
 * @param {led_info_t} *led_info
 * @param {led_ctrl_t} *led_ctrl
 * @return {bool}   {true 成功} {false 失败}
 */
bool led_toggle_level(led_info_t *led_info, led_ctrl_t *led_ctrl)
{
    led_ctrl->Current_Status = !led_ctrl->Current_Status;
    // return drv_led_on_off(led_info, led_ctrl);
    return true;
}

/**
 * @Author: YuluoY
 * @Date: 2024-10-30 19:50:47
 * @LastEditors: yuluoY 2120942398@qq.com
 * @LastEditTime: Do not edit
 * @Description: led参数设置
 * @param {led_ctrl_t} *led_ctrl    led控制结构体
 * @param {uint8_t} led_flag        led获取时间线标志位
 * @param {uint16_t} ontime         led打开的时间
 * @param {uint16_t} offtime        led关闭的时间
 * @param {uint8_t} frequency       led事件 0：关闭LED 1~254：LED闪烁的次数 255：当关闭时间为0时LED常亮，当关闭时间不为0时LED闪烁
 * @return {}
 */
void led_set_param(led_info_t *led_info, led_ctrl_t *led_ctrl, uint8_t led_flag, uint16_t ontime, uint16_t offtime, uint8_t frequency)
{
    led_ctrl->led_get_systemtimer_flag = led_flag;          /* led获取时间线标志位 */
    led_ctrl->led_blink_ontime = ontime;                    /* led打开的时间 */
    led_ctrl->led_blink_offtime = offtime;                  /* led关闭的时间 */
    led_ctrl->led_blink_frequency = frequency;              /* led事件 0：关闭LED 1~254：LED闪烁的次数 255：当关闭时间为0时LED常亮，当关闭时间不为0时LED闪烁 */

    led_ctrl->Current_Status = led_info->Led_Off_Level;     /* LED当前状态，永远为关闭 */
    led_ctrl->led_status = LED_OPEN;                        /* 闪烁时，打开作为第一状态 */
}

/**
 * @Author: YuluoY
 * @Date: 2024-10-29 22:43:17
 * @LastEditors: yuluoY 2120942398@qq.com
 * @LastEditTime: Do not edit
 * @Description: led运行任务
 * @return {}
 */
void led_task(void)
{
    led_control_function(&led_R_info, &LED_RED);
    led_control_function(&led_G_info, &LED_GREEN);
    drv_led_on_off(&led_R_info, &LED_RED);
    drv_led_on_off(&led_G_info, &LED_GREEN);
}

/**
 * @Author: YuluoY
 * @Date: 2024-10-31 20:26:41
 * @LastEditors: yuluoY 2120942398@qq.com
 * @LastEditTime: Do not edit
 * @Description: led控制函数
 * @param {led_info_t} *led_info
 * @param {led_ctrl_t} *led_ctrl
 * @return {}
 */
void led_control_function(led_info_t *led_info, led_ctrl_t *led_ctrl)
{
    if(led_ctrl->led_blink_frequency == LED_OPEN)   /* 闪烁次数最大，则一直闪烁 */
    {
        if(led_ctrl->led_blink_offtime == 0x00)
        {
            led_set_level(led_info, led_ctrl, !led_info->Led_Off_Level);
        }
        else
        {
            led_blink_function(led_info, led_ctrl, led_ctrl->led_blink_frequency);
        }
    }
    else if(led_ctrl->led_blink_frequency == LED_CLOSE) /* 闪烁次数为0，则关闭LED */
    {
        led_set_level(led_info, led_ctrl, led_info->Led_Off_Level);
    }
    else
    {
        led_blink_function(led_info, led_ctrl, led_ctrl->led_blink_frequency);
    }
}

/**
 * @Author: YuluoY
 * @Date: 2024-10-31 20:32:28
 * @LastEditors: yuluoY 2120942398@qq.com
 * @LastEditTime: Do not edit
 * @Description: led闪烁执行函数
 * @param {led_info_t} *led_info
 * @param {led_ctrl_t} *led_ctrl
 * @param {uint8_t} freq
 * @return {}
 */
void led_blink_function(led_info_t *led_info, led_ctrl_t *led_ctrl, uint8_t freq)
{
    uint8_t result = 0;
    switch (led_ctrl->led_status)
        {
        case LED_OPEN: /* 打开LED */
            if(!led_ctrl->led_get_systemtimer_flag)
            {
                led_ctrl->led_get_systemtimer_flag = true;
                led_ctrl->led_Current_Time = sys_timer.System_Timeline;
            }
            else
            {
                result = System_Time_Interval(led_ctrl->led_Current_Time, led_ctrl->led_blink_ontime);
                if(result)  /* 开启的时间到了*/
                {
                    led_ctrl->led_status = LED_CLOSE;
                    led_set_level(led_info, led_ctrl, led_info->Led_Off_Level);
                    led_ctrl->led_get_systemtimer_flag = false;
                }
                else
                {
                    led_set_level(led_info, led_ctrl, !led_info->Led_Off_Level);
                }
            }
            break;
        case LED_CLOSE: /* 关闭LED */
            if(!led_ctrl->led_get_systemtimer_flag)
            {
                led_ctrl->led_get_systemtimer_flag = true;
                led_ctrl->led_Current_Time = sys_timer.System_Timeline;
            }
            else
            {
                result = System_Time_Interval(led_ctrl->led_Current_Time, led_ctrl->led_blink_offtime);
                if(result)  /* 开启的时间到了*/
                {
                    if(led_ctrl->led_blink_frequency != LED_OPEN && led_ctrl->led_blink_frequency != LED_CLOSE)
                    {
                        led_ctrl->led_blink_frequency --;
                    } 
                    led_ctrl->led_status = LED_OPEN;
                    led_set_level(led_info, led_ctrl, !led_info->Led_Off_Level);
                    led_ctrl->led_get_systemtimer_flag = false;
                }
                else
                {
                    led_set_level(led_info, led_ctrl, led_info->Led_Off_Level);
                }
            }
            break;
        
        default:
            break;
        }
}
